package com.power.mock.util;


import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;


public class WJedisHelper {

	private static final String PREFIX="WCMC";
	private static ConcurrentMap<String, WJedisHelper> helperMap = new ConcurrentHashMap<String, WJedisHelper>();
	public static final String REDIS_MEM = "wcmc::mem";
	public static final Integer NEVER_TIMEOUT = -1;
	public static final Integer THREE_DAY = 60 * 60 * 24 * 3;
	public static final Integer ONE_HALF_DAY = 60 * 60 * 12;

	private WJedisHelper(String appName){
		this.appName = appName;
	}

	private final String appName;

	static {
		for (String appName : WRedisUtil.appNames) {
			helperMap.put(appName, new WJedisHelper(appName));
		}
	}

	public static WJedisHelper getHelper(String appName) {
		return helperMap.get(appName);
	}
	
	/**
	 * 
	 * 存储
	 * 
	 * @param key
	 *            目标key
	 * @param value
	 *            value
	 * @param seconds
	 *            过期时间，单位是秒
	 * @return
	 */
	public String set(final String key, final String value,
			final int seconds) {
		return WRedisUtil.<String>jedis(appName, new WJedisHandler<String>() {
			@Override
			public String handler(RedisAdaptor jedis) {
				if (seconds == -1) {
					return jedis.set(PREFIX + key, value);
				}
				return jedis.setex(PREFIX + key, seconds, value);
			}
		});
	}
	public Long setnx(final String key, final String value,
			final int seconds) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
					Long result= jedis.setnx(PREFIX + key, value);
					if(seconds!=-1&&result==1){
						jedis.expire(PREFIX + key, seconds);
					}
				    return result;
			}
		});
	}
	/**
	 * 向尾部添加
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public Long rpush(final String key, final String value,
			final int seconds) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				Long rpush = jedis.rpush(PREFIX + key, value);
				if(seconds!=-1){
					jedis.expire(PREFIX + key, seconds);
				}
				return rpush;
			}
		});
	}

	/**
	 * 向尾部添加,第一次插入时，返回的是1
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public Long rpushExist(final String key, final String value,
			final int seconds) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				Long rpush = jedis.rpush(PREFIX + key, value);
				if(seconds!=-1 && rpush==1){
					jedis.expire(PREFIX + key, seconds);
				}
				return rpush;
			}
		});
	}

	public Long incrby(final String key, final int num, final int seconds) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				Long incr = jedis.incrBy(PREFIX + key, num);
				if (incr == 1&&seconds!=-1) {
					jedis.expire(PREFIX + key, seconds);
				}
				return incr;
			}
		});
	}

	/**
	 * 向首部添加
	 * 
	 * @param key
	 * @param value
	 * @param seconds
	 * @return
	 */
	public Long lpush(final String key, final String value,
			final int seconds) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				Long rpush = jedis.lpush(PREFIX + key, value);
				jedis.expire(PREFIX + key, seconds);
				return rpush;
			}
		});
	}
	/**
	 * 删除队列里的元素
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Long lrem(final String key, final String value,final int count) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				Long rpush = jedis.lrem(PREFIX + key,count, value);
				return rpush;
			}
		});
	}

	/**
	 * 长度
	 * 
	 * @param key
	 * @return
	 */
	public Long llen(final String key) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.llen(PREFIX + key);
			}
		});
	}

	/**
	 * 
	 * 取出所有数据
	 * 
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public List<String> lrange(final String key, final long start, final long end) {
		return WRedisUtil.<List<String>>jedis(appName, new WJedisHandler<List<String>>() {
			@Override
			public List<String> handler(RedisAdaptor jedis) {
				return jedis.lrange(PREFIX + key, start, end);
			}
		});
	}

	public Long hset(final String key, final String field,
			final String value) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.hset(PREFIX + key, field, value);
			}
		});
	}

	public String hget(final String key, final String field) {
		return WRedisUtil.<String>jedis(appName, new WJedisHandler<String>() {
			@Override
			public String handler(RedisAdaptor jedis) {
				return jedis.hget(PREFIX + key, field);
			}
		});
	}

	public Map<String, String> hgetAll(final String key) {
		return WRedisUtil.<Map<String, String>>jedis(appName, new WJedisHandler<Map<String, String>>() {
			@Override
			public Map<String, String> handler(RedisAdaptor jedis) {
				return jedis.hgetAll(PREFIX + key);
			}
		});
	}

	public Long hdel(final String key, final String[] fields) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.hdel(PREFIX + key, fields);

			}
		});
	}

	public Long del(final String key) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.del(PREFIX + key);
			}
		});
	}
	
	public Long delWithoutPrefix(final String key) {
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.del(key);
			}
		});
	}

	/**
	 * 
	 * 从jedis加载数据
	 * 
	 * @param key
	 * @return
	 */
	public String get(final String key) {
		return WRedisUtil.<String>jedis(appName, new WJedisHandler<String>() {
			@Override
			public String handler(RedisAdaptor jedis) {
				return jedis.get(PREFIX + key);
			}
		});
	}
	
	/**
	 * 有续集添加
	 * @param key
	 * @param values
	 * @return
	 */
	public Long zadd(final String key,final Map<String, Double> values){
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.zadd(PREFIX + key, values);
			}
		});
	}
	
	/**
	 * 有续集降序分页
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public Set<String> zrevrange(final String key,final long start,final long end){
		return WRedisUtil.<Set<String>>jedis(appName, new WJedisHandler<Set<String>>() {
			
			@Override
			public Set<String> handler(RedisAdaptor jedis) {
				return jedis.zrevrange(PREFIX + key, start, end);
			}
		});
	}
	
	/**
	 * 获取key在降序状态下在有续集中的下标，下标从0开始
	 * @param set
	 * @param key
	 * @return
	 */
	public Long zrevrank(final String set,final String key){
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {

			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.zrevrank(PREFIX + set, key);
			}
			
		});
	}
	
	public Long zcount(final String set,final double min,final double max){
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.zcount(PREFIX + set, min, max);
			}
		});
	}
	
	public Long zcard(final String set){
		return WRedisUtil.<Long>jedis(appName, new WJedisHandler<Long>() {
			@Override
			public Long handler(RedisAdaptor jedis) {
				return jedis.zcard(PREFIX + set);
			}
		});
	}

	public Set<String> zrange(final String set,final long start,final long end){
		return WRedisUtil.<Set<String>>jedis(appName, new WJedisHandler<Set<String>>() {
			@Override
			public Set<String> handler(RedisAdaptor jedis) {
				return jedis.zrange(PREFIX + set, start, end);
			}
		});
	}

	/**
	 * 从首部出栈
	 *
	 * @param key
	 * @return
	 */
	public String lpop(final String key) {
		return WRedisUtil.<String>jedis(appName, new WJedisHandler<String>() {
			@Override
			public String handler(RedisAdaptor jedis) {
				String value = jedis.lpop(PREFIX + key);
				return value;
			}
		});
	}

	/**
	 * 从尾部出栈
	 *
	 * @param key
	 * @return
	 */
	public String rpop(final String key) {
		return WRedisUtil.<String>jedis(appName, new WJedisHandler<String>() {
			@Override
			public String handler(RedisAdaptor jedis) {
				String value = jedis.rpop(PREFIX + key);
				return value;
			}
		});
	}

	/**
	 * 批量get
	 * @param keys
	 * @return
	 */
	public List<String> mget(final String[] keys) {
		return WRedisUtil.jedis(appName, new WJedisHandler<List<String>>() {
			@Override
			public List<String> handler(RedisAdaptor wjedis) {
				return wjedis.mget(keys);
			}

		});
	}



}
